#include "reader.h"
#include "library.h"

Reader::Reader(Library * lib, int readTime) : lib(lib), readTime(readTime)
{
    lib->registerPerson();
}

void Reader::run()
{
    while(true) {
        if(lib->isClosed()) return;

        if (lib->getBooksCount() == 0) {
            usleep(readTime);
            continue;
        }
        int index = random() % lib->getBooksCount();
        if (lib->tryAcquireBook(index)) {
            usleep(readTime);
            lib->releaseBook(index);
        }
    }
}
